<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>nginx配置参考 | 눈_눈？</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="nginx配置参考">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.39493abb.css" as="style"><link rel="preload" href="/assets/js/app.f4a44a18.js" as="script"><link rel="preload" href="/assets/js/3.48232c0c.js" as="script"><link rel="preload" href="/assets/js/1.1ffd113d.js" as="script"><link rel="preload" href="/assets/js/32.4e2b272d.js" as="script"><link rel="prefetch" href="/assets/js/10.3ca9dec1.js"><link rel="prefetch" href="/assets/js/11.874c1055.js"><link rel="prefetch" href="/assets/js/12.ba64ad86.js"><link rel="prefetch" href="/assets/js/13.567f7e2c.js"><link rel="prefetch" href="/assets/js/14.a883120b.js"><link rel="prefetch" href="/assets/js/15.f0f48289.js"><link rel="prefetch" href="/assets/js/16.665ca6bd.js"><link rel="prefetch" href="/assets/js/17.9e5d8903.js"><link rel="prefetch" href="/assets/js/18.f72895dc.js"><link rel="prefetch" href="/assets/js/19.2a106cc2.js"><link rel="prefetch" href="/assets/js/20.4c0d7dc8.js"><link rel="prefetch" href="/assets/js/21.6771e3a2.js"><link rel="prefetch" href="/assets/js/22.f3c7aad6.js"><link rel="prefetch" href="/assets/js/23.60529100.js"><link rel="prefetch" href="/assets/js/24.7d50cb5e.js"><link rel="prefetch" href="/assets/js/25.58231492.js"><link rel="prefetch" href="/assets/js/26.3067a077.js"><link rel="prefetch" href="/assets/js/27.b167830e.js"><link rel="prefetch" href="/assets/js/28.f5722284.js"><link rel="prefetch" href="/assets/js/29.62d2e4fe.js"><link rel="prefetch" href="/assets/js/30.c2afa3b0.js"><link rel="prefetch" href="/assets/js/31.d7dde24d.js"><link rel="prefetch" href="/assets/js/33.5e4d7960.js"><link rel="prefetch" href="/assets/js/34.8d5624e0.js"><link rel="prefetch" href="/assets/js/35.abd48ffb.js"><link rel="prefetch" href="/assets/js/36.e231ea32.js"><link rel="prefetch" href="/assets/js/37.e99c1b8f.js"><link rel="prefetch" href="/assets/js/38.ff825294.js"><link rel="prefetch" href="/assets/js/39.b3321103.js"><link rel="prefetch" href="/assets/js/4.936c6377.js"><link rel="prefetch" href="/assets/js/40.8e915e21.js"><link rel="prefetch" href="/assets/js/41.9a5c6a3d.js"><link rel="prefetch" href="/assets/js/42.2159b3c3.js"><link rel="prefetch" href="/assets/js/43.144bc67d.js"><link rel="prefetch" href="/assets/js/44.c7b1a0a9.js"><link rel="prefetch" href="/assets/js/45.fafbd061.js"><link rel="prefetch" href="/assets/js/46.dfd415c1.js"><link rel="prefetch" href="/assets/js/47.d6285fa6.js"><link rel="prefetch" href="/assets/js/48.c3c90153.js"><link rel="prefetch" href="/assets/js/49.fa7ab119.js"><link rel="prefetch" href="/assets/js/5.0bd232c4.js"><link rel="prefetch" href="/assets/js/50.6d5fe086.js"><link rel="prefetch" href="/assets/js/51.e0b9a5c9.js"><link rel="prefetch" href="/assets/js/52.39d71cb2.js"><link rel="prefetch" href="/assets/js/53.ad47bc0c.js"><link rel="prefetch" href="/assets/js/54.989d4596.js"><link rel="prefetch" href="/assets/js/55.fea0aaad.js"><link rel="prefetch" href="/assets/js/56.af246d88.js"><link rel="prefetch" href="/assets/js/57.747bce1d.js"><link rel="prefetch" href="/assets/js/58.8d177768.js"><link rel="prefetch" href="/assets/js/59.e97890fd.js"><link rel="prefetch" href="/assets/js/6.8aac6fc8.js"><link rel="prefetch" href="/assets/js/60.114e9ca6.js"><link rel="prefetch" href="/assets/js/7.f1ea8070.js"><link rel="prefetch" href="/assets/js/8.af678f34.js"><link rel="prefetch" href="/assets/js/9.a1b28891.js">
    <link rel="stylesheet" href="/assets/css/0.styles.39493abb.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div><div class="theme-container" data-v-e3b9d544><div data-v-e3b9d544><div id="loader-wrapper" class="loading-wrapper" data-v-1c4f0192 data-v-e3b9d544 data-v-e3b9d544><div class="loader-main" data-v-1c4f0192><div data-v-1c4f0192></div><div data-v-1c4f0192></div><div data-v-1c4f0192></div><div data-v-1c4f0192></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-071f7da2 data-v-e3b9d544 data-v-e3b9d544><h3 class="title" style="display:none;" data-v-071f7da2 data-v-071f7da2>눈_눈？</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-071f7da2 data-v-071f7da2><input type="password" value="" data-v-071f7da2> <span data-v-071f7da2>Konck! Knock!</span> <button data-v-071f7da2>OK</button></label> <div class="footer" style="display:none;" data-v-071f7da2 data-v-071f7da2><span data-v-071f7da2><i class="iconfont reco-theme" data-v-071f7da2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-071f7da2>vuePress-theme-reco</a></span> <span data-v-071f7da2><i class="iconfont reco-other" data-v-071f7da2></i> <a data-v-071f7da2>yykrlc</a></span> <span data-v-071f7da2><i class="iconfont reco-copyright" data-v-071f7da2></i> <a data-v-071f7da2>2021</a></span></div></div> <div class="hide" data-v-e3b9d544><header class="navbar" data-v-e3b9d544><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/head.png" alt="눈_눈？" class="logo"> <span class="site-name">눈_눈？</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/朗读/" class="nav-link"><i class="iconfont undefined"></i>
  朗读
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="iconfont undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Docker/" class="nav-link"><i class="iconfont undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/sql/" class="nav-link"><i class="iconfont undefined"></i>
  sql
</a></li><li class="dropdown-item"><!----> <a href="/categories/其他/" class="nav-link"><i class="iconfont undefined"></i>
  其他
</a></li><li class="dropdown-item"><!----> <a href="/categories/Other/" class="nav-link"><i class="iconfont undefined"></i>
  Other
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue/" class="nav-link"><i class="iconfont undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/categories/Css/" class="nav-link"><i class="iconfont undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/闲言碎语/" class="nav-link"><i class="iconfont undefined"></i>
  闲言碎语
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/tools/" class="nav-link"><i class="iconfont reco-document"></i>
  收藏
</a></div><div class="nav-item"><a href="/photo/" class="nav-link"><i class="iconfont reco-douyin"></i>
  画廊
</a></div><div class="nav-item"><a href="/friends/" class="nav-link"><i class="iconfont reco-friend"></i>
  关于
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-e3b9d544></div> <aside class="sidebar" data-v-e3b9d544><div class="personal-info-wrapper" data-v-c115f482><img src="/assets/img/home-head.32578f7e.png" alt="hero" class="personal-img" data-v-c115f482> <h3 class="name" data-v-c115f482>yykrlc</h3> <div class="num" data-v-c115f482><div data-v-c115f482><h3 data-v-c115f482>44</h3> <h6 data-v-c115f482>文章</h6></div> <div data-v-c115f482><h3 data-v-c115f482>27</h3> <h6 data-v-c115f482>标签</h6></div></div> <hr data-v-c115f482></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/朗读/" class="nav-link"><i class="iconfont undefined"></i>
  朗读
</a></li><li class="dropdown-item"><!----> <a href="/categories/工具/" class="nav-link"><i class="iconfont undefined"></i>
  工具
</a></li><li class="dropdown-item"><!----> <a href="/categories/Docker/" class="nav-link"><i class="iconfont undefined"></i>
  Docker
</a></li><li class="dropdown-item"><!----> <a href="/categories/Linux/" class="nav-link"><i class="iconfont undefined"></i>
  Linux
</a></li><li class="dropdown-item"><!----> <a href="/categories/sql/" class="nav-link"><i class="iconfont undefined"></i>
  sql
</a></li><li class="dropdown-item"><!----> <a href="/categories/其他/" class="nav-link"><i class="iconfont undefined"></i>
  其他
</a></li><li class="dropdown-item"><!----> <a href="/categories/Other/" class="nav-link"><i class="iconfont undefined"></i>
  Other
</a></li><li class="dropdown-item"><!----> <a href="/categories/python/" class="nav-link"><i class="iconfont undefined"></i>
  python
</a></li><li class="dropdown-item"><!----> <a href="/categories/Vue/" class="nav-link"><i class="iconfont undefined"></i>
  Vue
</a></li><li class="dropdown-item"><!----> <a href="/categories/Css/" class="nav-link"><i class="iconfont undefined"></i>
  Css
</a></li><li class="dropdown-item"><!----> <a href="/categories/JavaScript/" class="nav-link"><i class="iconfont undefined"></i>
  JavaScript
</a></li><li class="dropdown-item"><!----> <a href="/categories/闲言碎语/" class="nav-link"><i class="iconfont undefined"></i>
  闲言碎语
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  标签
</a></div><div class="nav-item"><a href="/tools/" class="nav-link"><i class="iconfont reco-document"></i>
  收藏
</a></div><div class="nav-item"><a href="/photo/" class="nav-link"><i class="iconfont reco-douyin"></i>
  画廊
</a></div><div class="nav-item"><a href="/friends/" class="nav-link"><i class="iconfont reco-friend"></i>
  关于
</a></div><div class="nav-item"><a href="/timeLine/" class="nav-link"><i class="iconfont reco-date"></i>
  时间轴
</a></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>nginx配置参考</span> <!----></p> <!----></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-071f7da2 data-v-e3b9d544><h3 class="title" style="display:none;" data-v-071f7da2 data-v-071f7da2>nginx配置参考</h3> <!----> <label id="box" class="inputBox" style="display:none;" data-v-071f7da2 data-v-071f7da2><input type="password" value="" data-v-071f7da2> <span data-v-071f7da2>Konck! Knock!</span> <button data-v-071f7da2>OK</button></label> <div class="footer" style="display:none;" data-v-071f7da2 data-v-071f7da2><span data-v-071f7da2><i class="iconfont reco-theme" data-v-071f7da2></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-071f7da2>vuePress-theme-reco</a></span> <span data-v-071f7da2><i class="iconfont reco-other" data-v-071f7da2></i> <a data-v-071f7da2>yykrlc</a></span> <span data-v-071f7da2><i class="iconfont reco-copyright" data-v-071f7da2></i> <a data-v-071f7da2>2021</a></span></div></div> <div data-v-e3b9d544><main class="page"><!----> <div class="page-title" style="display:none;"><h1>nginx配置参考</h1> <hr> <div data-v-3cf1d7fb><i class="iconfont reco-account" data-v-3cf1d7fb><span data-v-3cf1d7fb>yykrlc</span></i> <i class="iconfont reco-date" data-v-3cf1d7fb><span data-v-3cf1d7fb>2019-12-18</span></i> <i class="iconfont reco-eye" data-v-3cf1d7fb><span id="/views/linux/nginx%E9%85%8D%E7%BD%AE%E5%A4%87%E6%B3%A8.html" data-flag-title="Your Article Title" class="leancloud-visitors" data-v-3cf1d7fb><a class="leancloud-visitors-count" style="font-size:.9rem;font-weight:normal;color:#999;"></a></span></i> <i class="iconfont reco-tag tags" data-v-3cf1d7fb><span class="tag-item" data-v-3cf1d7fb>
      nginx
    </span></i></div></div> <div class="theme-reco-content content__default" style="display:none;"><h3 id="nginx配置参数中文详细说明"><a href="#nginx配置参数中文详细说明" class="header-anchor">#</a> Nginx配置参数中文详细说明：</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment">#定义Nginx运行的用户和用户组</span>
<span class="token keyword">user</span> www www<span class="token punctuation">;</span>
<span class="token comment">#</span>
<span class="token comment">#nginx进程数,建议设置为等于CPU总核心数.</span>
<span class="token keyword">worker_processes</span> <span class="token number">8</span><span class="token punctuation">;</span>
<span class="token comment">#</span>
<span class="token comment">#全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]</span>
<span class="token keyword">error_log</span> <span class="token operator">/</span>var<span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>error<span class="token punctuation">.</span>log info<span class="token punctuation">;</span>
<span class="token comment">#</span>
<span class="token comment">#进程文件</span>
<span class="token keyword">pid</span> <span class="token operator">/</span>var<span class="token operator">/</span>run<span class="token operator">/</span>nginx<span class="token punctuation">.</span><span class="token keyword">pid</span><span class="token punctuation">;</span>
<span class="token comment">#</span>
<span class="token comment">#一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数（系统的值ulimit -n）与nginx进程数相除,但是nginx分配请求并不均匀,所以建议与ulimit -n的值保持一致.</span>
<span class="token keyword">worker_rlimit_nofile</span> <span class="token number">65535</span><span class="token punctuation">;</span>
<span class="token comment">#</span>
<span class="token comment">#工作模式与连接数上限</span>
<span class="token keyword">events</span>
<span class="token punctuation">{</span>
    <span class="token comment">#参考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本内核中的高性能网络I/O模型,如果跑在FreeBSD上面,就用kqueue模型.</span>
    <span class="token keyword">use</span> epoll<span class="token punctuation">;</span>
    <span class="token comment">#单个进程最大连接数（最大连接数=连接数*进程数）</span>
    <span class="token keyword">worker_connections</span> <span class="token number">65535</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">#</span>
<span class="token comment">#设定http服务器</span>
<span class="token keyword">http</span>
<span class="token punctuation">{</span>
    <span class="token keyword">include</span> mime<span class="token punctuation">.</span><span class="token keyword">types</span><span class="token punctuation">;</span> <span class="token comment">#文件扩展名与文件类型映射表</span>
    <span class="token keyword">default_type</span> application<span class="token operator">/</span>octet<span class="token operator">-</span>stream<span class="token punctuation">;</span> <span class="token comment">#默认文件类型</span>
    <span class="token comment">#charset utf-8; #默认编码</span>
    <span class="token keyword">server_names_hash_bucket_size</span> <span class="token number">128</span><span class="token punctuation">;</span> <span class="token comment">#服务器名字的hash表大小</span>
    <span class="token keyword">client_header_buffer_size</span> <span class="token number">32</span>k<span class="token punctuation">;</span> <span class="token comment">#上传文件大小限制</span>
    <span class="token keyword">large_client_header_buffers</span> <span class="token number">4</span> <span class="token number">64</span>k<span class="token punctuation">;</span> <span class="token comment">#设定请求缓</span>
    <span class="token keyword">client_max_body_size</span> <span class="token number">8</span>m<span class="token punctuation">;</span> <span class="token comment">#设定请求缓</span>
    
    <span class="token comment"># 开启目录列表访问,合适下载服务器,默认关闭.</span>
    <span class="token keyword">autoindex</span> on<span class="token punctuation">;</span> <span class="token comment"># 显示目录</span>
    <span class="token keyword">autoindex_exact_size</span> on<span class="token punctuation">;</span> <span class="token comment"># 显示文件大小 默认为on,显示出文件的确切大小,单位是bytes 改为off后,显示出文件的大概大小,单位是kB或者MB或者GB</span>
    <span class="token keyword">autoindex_localtime</span> on<span class="token punctuation">;</span> <span class="token comment"># 显示文件时间 默认为off,显示的文件时间为GMT时间 改为on后,显示的文件时间为文件的服务器时间</span>
    
    <span class="token keyword">sendfile</span> on<span class="token punctuation">;</span> <span class="token comment"># 开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载.注意：如果图片显示不正常把这个改成off.</span>
    <span class="token keyword">tcp_nopush</span> on<span class="token punctuation">;</span> <span class="token comment"># 防止网络阻塞</span>
    <span class="token keyword">tcp_nodelay</span> on<span class="token punctuation">;</span> <span class="token comment"># 防止网络阻塞</span>
    
    <span class="token keyword">keepalive_timeout</span> <span class="token number">120</span><span class="token punctuation">;</span> <span class="token comment"># (单位s)设置客户端连接保持活动的超时时间,在超过这个时间后服务器会关闭该链接</span>
    
    <span class="token comment"># FastCGI相关参数是为了改善网站的性能：减少资源占用,提高访问速度.下面参数看字面意思都能理解.</span>
    <span class="token keyword">fastcgi_connect_timeout</span> <span class="token number">300</span><span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_send_timeout</span> <span class="token number">300</span><span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_read_timeout</span> <span class="token number">300</span><span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_buffer_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_buffers</span> <span class="token number">4</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_busy_buffers_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span>
    <span class="token keyword">fastcgi_temp_file_write_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span>
    
    <span class="token comment"># gzip模块设置</span>
    <span class="token keyword">gzip</span> on<span class="token punctuation">;</span> <span class="token comment">#开启gzip压缩输出</span>
    <span class="token keyword">gzip_min_length</span> <span class="token number">1</span>k<span class="token punctuation">;</span> <span class="token comment">#允许压缩的页面的最小字节数,页面字节数从header偷得content-length中获取.默认是0,不管页面多大都进行压缩.建议设置成大于1k的字节数,小于1k可能会越压越大</span>
    <span class="token keyword">gzip_buffers</span> <span class="token number">4</span> <span class="token number">16</span>k<span class="token punctuation">;</span> <span class="token comment">#表示申请4个单位为16k的内存作为压缩结果流缓存,默认值是申请与原始数据大小相同的内存空间来存储gzip压缩结果</span>
    <span class="token keyword">gzip_http_version</span> <span class="token number">1.1</span><span class="token punctuation">;</span> <span class="token comment">#压缩版本（默认1.1,目前大部分浏览器已经支持gzip解压.前端如果是squid2.5请使用1.0）</span>
    <span class="token keyword">gzip_comp_level</span> <span class="token number">2</span><span class="token punctuation">;</span> <span class="token comment">#压缩等级.1压缩比最小,处理速度快.9压缩比最大,比较消耗cpu资源,处理速度最慢,但是因为压缩比最大,所以包最小,传输速度快</span>
    <span class="token keyword">gzip_types</span> text<span class="token operator">/</span>plain application<span class="token operator">/</span>x<span class="token operator">-</span>javascript text<span class="token operator">/</span>css application<span class="token operator">/</span>xml<span class="token punctuation">;</span>
    <span class="token comment">#压缩类型,默认就已经包含text/html,所以下面就不用再写了,写上去也不会有问题,但是会有一个warn.</span>
    <span class="token keyword">gzip_vary</span> on<span class="token punctuation">;</span><span class="token comment">#选项可以让前端的缓存服务器缓存经过gzip压缩的页面.例如:用squid缓存经过nginx压缩的数据</span>
    
    <span class="token comment">#开启限制IP连接数的时候需要使用</span>
    <span class="token comment">#limit_zone crawler $binary_remote_addr 10m;</span>
    
    <span class="token comment">##upstream的负载均衡,四种调度算法(下例主讲)##</span>
    
    <span class="token comment">#虚拟主机的配置</span>
    <span class="token keyword">server</span>
    <span class="token punctuation">{</span>
        <span class="token comment"># 监听端口</span>
        <span class="token keyword">listen</span> <span class="token number">80</span><span class="token punctuation">;</span>
        <span class="token comment"># 域名可以有多个,用空格隔开</span>
        <span class="token keyword">server_name</span> ably<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        <span class="token comment"># HTTP 自动跳转 HTTPS</span>
        <span class="token keyword">rewrite</span> <span class="token operator">^</span><span class="token punctuation">(</span><span class="token punctuation">.</span><span class="token operator">*</span><span class="token punctuation">)</span> <span class="token keyword">https</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span><span class="token variable">$server_name</span>$<span class="token number">1</span> permanent<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">server</span>
    <span class="token punctuation">{</span>
        <span class="token comment"># 监听端口 HTTPS</span>
        <span class="token keyword">listen</span> <span class="token number">443</span> <span class="token keyword">ssl</span><span class="token punctuation">;</span>
        <span class="token keyword">server_name</span> ably<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        
        <span class="token comment"># 配置域名证书</span>
        <span class="token keyword">ssl_certificate</span>      C<span class="token punctuation">:</span>\WebServer\Certs\certificate<span class="token punctuation">.</span>crt<span class="token punctuation">;</span>
        <span class="token keyword">ssl_certificate_key</span>  C<span class="token punctuation">:</span>\WebServer\Certs\private<span class="token punctuation">.</span>key<span class="token punctuation">;</span>
        <span class="token keyword">ssl_session_cache</span>    shared<span class="token punctuation">:</span><span class="token keyword">SSL</span><span class="token punctuation">:</span><span class="token number">1</span>m<span class="token punctuation">;</span>
        <span class="token keyword">ssl_session_timeout</span>  <span class="token number">5</span>m<span class="token punctuation">;</span>
        <span class="token keyword">ssl_protocols</span> SSLv2 SSLv3 TLSv1<span class="token punctuation">;</span>
        <span class="token keyword">ssl_ciphers</span> ALL<span class="token punctuation">:</span><span class="token operator">!</span>ADH<span class="token punctuation">:</span><span class="token operator">!</span>EXPORT56<span class="token punctuation">:</span>RC4<span class="token operator">+</span>RSA<span class="token punctuation">:</span><span class="token operator">+</span>HIGH<span class="token punctuation">:</span><span class="token operator">+</span>MEDIUM<span class="token punctuation">:</span><span class="token operator">+</span>LOW<span class="token punctuation">:</span><span class="token operator">+</span>SSLv2<span class="token punctuation">:</span><span class="token operator">+</span>EXP<span class="token punctuation">;</span>
        <span class="token keyword">ssl_prefer_server_ciphers</span>  on<span class="token punctuation">;</span>
    
        <span class="token keyword">index</span> <span class="token keyword">index</span><span class="token punctuation">.</span>html <span class="token keyword">index</span><span class="token punctuation">.</span>htm <span class="token keyword">index</span><span class="token punctuation">.</span>php<span class="token punctuation">;</span>
        <span class="token keyword">root</span> <span class="token operator">/</span>data<span class="token operator">/</span>www<span class="token operator">/</span><span class="token punctuation">;</span>
        <span class="token keyword">location</span> <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>php<span class="token operator">|</span>php5<span class="token punctuation">)</span><span class="token operator">?</span>$
        <span class="token punctuation">{</span>
            <span class="token keyword">fastcgi_pass</span> <span class="token number">127.0</span><span class="token number">.0</span><span class="token number">.1</span><span class="token punctuation">:</span><span class="token number">9000</span><span class="token punctuation">;</span>
            <span class="token keyword">fastcgi_index</span> <span class="token keyword">index</span><span class="token punctuation">.</span>php<span class="token punctuation">;</span>
            <span class="token keyword">include</span> fastcgi<span class="token punctuation">.</span>conf<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment"># 配置地址拦截转发，解决跨域验证问题</span>
        <span class="token keyword">location</span> <span class="token operator">/</span>oauth<span class="token operator">/</span><span class="token punctuation">{</span>
            <span class="token keyword">proxy_pass</span> <span class="token keyword">https</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token punctuation">:</span><span class="token number">13580</span><span class="token operator">/</span>oauth<span class="token operator">/</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> HOST <span class="token variable">$host</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment"># 图片缓存时间设置</span>
        <span class="token keyword">location</span> <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>gif<span class="token operator">|</span>jpg<span class="token operator">|</span>jpeg<span class="token operator">|</span>png<span class="token operator">|</span>bmp<span class="token operator">|</span>swf<span class="token punctuation">)</span>$ <span class="token punctuation">{</span>
            <span class="token keyword">expires</span> <span class="token number">10</span>d<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        
        <span class="token comment"># JS和CSS缓存时间设置</span>
        <span class="token keyword">location</span> <span class="token operator">~</span> <span class="token punctuation">.</span><span class="token operator">*</span>\<span class="token punctuation">.</span><span class="token punctuation">(</span>js<span class="token operator">|</span>css<span class="token punctuation">)</span><span class="token operator">?</span>$ <span class="token punctuation">{</span>
            <span class="token keyword">expires</span> <span class="token number">1</span>h<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment"># 日志格式设定</span>
        <span class="token keyword">log_format</span> access <span class="token string">'$remote_addr - $remote_user [$time_local] &quot;$request&quot; '</span>
        <span class="token string">'$status $body_bytes_sent &quot;$http_referer&quot; '</span>
        <span class="token string">'&quot;$http_user_agent&quot; $http_x_forwarded_for'</span><span class="token punctuation">;</span>
        <span class="token comment"># 定义本虚拟主机的访问日志</span>
        <span class="token keyword">access_log</span> <span class="token operator">/</span>var<span class="token operator">/</span>log<span class="token operator">/</span>nginx<span class="token operator">/</span>access<span class="token punctuation">.</span>log access<span class="token punctuation">;</span>
        
        <span class="token comment"># 设定查看Nginx状态的地址.StubStatus模块能够获取Nginx自上次启动以来的工作状态，此模块非核心模块，需要在Nginx编译安装时手工指定才能使用</span>
        <span class="token keyword">location</span> <span class="token operator">/</span>NginxStatus <span class="token punctuation">{</span>
            <span class="token keyword">stub_status</span> on<span class="token punctuation">;</span>
            <span class="token keyword">access_log</span> on<span class="token punctuation">;</span>
            <span class="token keyword">auth_basic</span> <span class="token string">&quot;NginxStatus&quot;</span><span class="token punctuation">;</span>
            <span class="token keyword">auth_basic_user_file</span> conf<span class="token operator">/</span>htpasswd<span class="token punctuation">;</span>
            <span class="token comment">#htpasswd文件的内容可以用apache提供的htpasswd工具来产生.</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br><span class="line-number">105</span><br><span class="line-number">106</span><br><span class="line-number">107</span><br><span class="line-number">108</span><br><span class="line-number">109</span><br><span class="line-number">110</span><br><span class="line-number">111</span><br><span class="line-number">112</span><br><span class="line-number">113</span><br><span class="line-number">114</span><br><span class="line-number">115</span><br><span class="line-number">116</span><br><span class="line-number">117</span><br><span class="line-number">118</span><br><span class="line-number">119</span><br><span class="line-number">120</span><br><span class="line-number">121</span><br><span class="line-number">122</span><br><span class="line-number">123</span><br><span class="line-number">124</span><br><span class="line-number">125</span><br><span class="line-number">126</span><br><span class="line-number">127</span><br><span class="line-number">128</span><br><span class="line-number">129</span><br><span class="line-number">130</span><br><span class="line-number">131</span><br><span class="line-number">132</span><br><span class="line-number">133</span><br><span class="line-number">134</span><br><span class="line-number">135</span><br><span class="line-number">136</span><br><span class="line-number">137</span><br><span class="line-number">138</span><br><span class="line-number">139</span><br><span class="line-number">140</span><br></div></div><p>Nginx多台服务器实现负载均衡：
1.Nginx负载均衡服务器
IP：192.168.0.4（Nginx-Server）
2.Web服务器列表
Web1:192.168.0.5（Nginx-Node1/Nginx-Web1） ；
Web2:192.168.0.7（Nginx-Node2/Nginx-Web2）
3.实现目的
用户访问Nginx-Server（“http://mongo.demo.com:8888”）时，通过Nginx负载均衡到Web1和Web2服务器
Nginx负载均衡服务器的nginx.conf配置注释如下：</p> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token keyword">events</span>
<span class="token punctuation">{</span>
    <span class="token keyword">use</span> epoll<span class="token punctuation">;</span>
    <span class="token keyword">worker_connections</span> <span class="token number">65535</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">http</span>
<span class="token punctuation">{</span>
    <span class="token comment">##upstream的负载均衡,四种调度算法##</span>
    <span class="token comment">#调度算法1:轮询.每个请求按时间顺序逐一分配到不同的后端服务器,如果后端某台服务器宕机,故障系统被自动剔除,使用户访问不受影响</span>
    <span class="token keyword">upstream</span> webhost <span class="token punctuation">{</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.5</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.7</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">#调度算法2:weight(权重).可以根据机器配置定义权重.权重越高被分配到的几率越大</span>
    <span class="token keyword">upstream</span> webhost <span class="token punctuation">{</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.5</span><span class="token punctuation">:</span><span class="token number">6666</span> weight<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.7</span><span class="token punctuation">:</span><span class="token number">6666</span> weight<span class="token operator">=</span><span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">#调度算法3:ip_hash. 每个请求按访问IP的hash结果分配,这样来自同一个IP的访客固定访问一个后端服务器,有效解决了动态网页存在的session共享问题</span>
    <span class="token keyword">upstream</span> webhost <span class="token punctuation">{</span>
        <span class="token keyword">ip_hash</span><span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.5</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.7</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">#调度算法4:url_hash(需安装第三方插件).此方法按访问url的hash结果来分配请求,使每个url定向到同一个后端服务器,可以进一步提高后端缓存服务器的效率.Nginx本身是不支持url_hash的,如果需要使用这种调度算法,必须安装Nginx 的hash软件包</span>
    <span class="token keyword">upstream</span> webhost <span class="token punctuation">{</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.5</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.7</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
        hash <span class="token variable">$request_uri</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">#调度算法5:fair(需安装第三方插件).这是比上面两个更加智能的负载均衡算法.此种算法可以依据页面大小和加载时间长短智能地进行负载均衡,也就是根据后端服务器的响应时间来分配请求,响应时间短的优先分配.Nginx本身是不支持fair的,如果需要使用这种调度算法,必须下载Nginx的upstream_fair模块</span>
    <span class="token comment">#</span>
    <span class="token comment">#虚拟主机的配置(采用调度算法3:ip_hash)</span>
    <span class="token keyword">server</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">listen</span> <span class="token number">80</span><span class="token punctuation">;</span>
        <span class="token keyword">server_name</span> mongo<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        <span class="token comment">#对 &quot;/&quot; 启用反向代理</span>
        <span class="token keyword">location</span> <span class="token operator">/</span> <span class="token punctuation">{</span>
            <span class="token keyword">proxy_pass</span> <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>webhost<span class="token punctuation">;</span>
            <span class="token keyword">proxy_redirect</span> off<span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
            <span class="token comment">#后端的Web服务器可以通过X-Forwarded-For获取用户真实IP</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span>
            <span class="token comment">#以下是一些反向代理的配置,可选.</span>
            <span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span><span class="token punctuation">;</span>
            <span class="token keyword">client_max_body_size</span> <span class="token number">10</span>m<span class="token punctuation">;</span> <span class="token comment">#允许客户端请求的最大单文件字节数</span>
            <span class="token keyword">client_body_buffer_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span> <span class="token comment">#缓冲区代理缓冲用户端请求的最大字节数,</span>
            <span class="token keyword">proxy_connect_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span> <span class="token comment">#nginx跟后端服务器连接超时时间(代理连接超时)</span>
            <span class="token keyword">proxy_send_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span> <span class="token comment">#后端服务器数据回传时间(代理发送超时)</span>
            <span class="token keyword">proxy_read_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span> <span class="token comment">#连接成功后,后端服务器响应时间(代理接收超时)</span>
            <span class="token keyword">proxy_buffer_size</span> <span class="token number">4</span>k<span class="token punctuation">;</span> <span class="token comment">#设置代理服务器（nginx）保存用户头信息的缓冲区大小</span>
            <span class="token keyword">proxy_buffers</span> <span class="token number">4</span> <span class="token number">32</span>k<span class="token punctuation">;</span> <span class="token comment">#proxy_buffers缓冲区,网页平均在32k以下的设置</span>
            <span class="token keyword">proxy_busy_buffers_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span> <span class="token comment">#高负荷下缓冲大小（proxy_buffers*2）</span>
            <span class="token keyword">proxy_temp_file_write_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
            <span class="token comment">#设定缓存文件夹大小,大于这个值,将从upstream服务器传</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br></div></div><h3 id="负载均衡操作演示如下-操作对象-192-168-0-4-nginx-server"><a href="#负载均衡操作演示如下-操作对象-192-168-0-4-nginx-server" class="header-anchor">#</a> 负载均衡操作演示如下：操作对象：192.168.0.4（Nginx-Server）</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment"># 创建文件夹准备存放配置文件</span>
$ mkdir <span class="token operator">-</span>p <span class="token operator">/</span>opt<span class="token operator">/</span>confs
$ vim <span class="token operator">/</span>opt<span class="token operator">/</span>confs<span class="token operator">/</span>nginx<span class="token punctuation">.</span>conf

<span class="token comment"># 编辑内容如下：</span>
<span class="token keyword">events</span>
<span class="token punctuation">{</span>
  <span class="token keyword">use</span> epoll<span class="token punctuation">;</span>
  <span class="token keyword">worker_connections</span> <span class="token number">65535</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">http</span>
<span class="token punctuation">{</span>
    <span class="token keyword">upstream</span> webhost <span class="token punctuation">{</span>
        <span class="token keyword">ip_hash</span><span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.5</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
        <span class="token keyword">server</span> <span class="token number">192.168</span><span class="token number">.0</span><span class="token number">.7</span><span class="token punctuation">:</span><span class="token number">6666</span> <span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
    <span class="token keyword">server</span>
    <span class="token punctuation">{</span>
        <span class="token keyword">listen</span> <span class="token number">80</span><span class="token punctuation">;</span>
        <span class="token keyword">server_name</span> mongo<span class="token punctuation">.</span>demo<span class="token punctuation">.</span>com<span class="token punctuation">;</span>
        <span class="token keyword">location</span> <span class="token operator">/</span> <span class="token punctuation">{</span>
            <span class="token keyword">proxy_pass</span> <span class="token keyword">http</span><span class="token punctuation">:</span><span class="token operator">/</span><span class="token operator">/</span>webhost<span class="token punctuation">;</span>
            <span class="token keyword">proxy_redirect</span> off<span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Real<span class="token operator">-</span>IP <span class="token variable">$remote_addr</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> X<span class="token operator">-</span>Forwarded<span class="token operator">-</span>For <span class="token variable">$proxy_add_x_forwarded_for</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_set_header</span> Host <span class="token variable">$host</span><span class="token punctuation">;</span>
            <span class="token keyword">client_max_body_size</span> <span class="token number">10</span>m<span class="token punctuation">;</span>
            <span class="token keyword">client_body_buffer_size</span> <span class="token number">128</span>k<span class="token punctuation">;</span>
            <span class="token keyword">proxy_connect_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_send_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_read_timeout</span> <span class="token number">90</span><span class="token punctuation">;</span>
            <span class="token keyword">proxy_buffer_size</span> <span class="token number">4</span>k<span class="token punctuation">;</span>
            <span class="token keyword">proxy_buffers</span> <span class="token number">4</span> <span class="token number">32</span>k<span class="token punctuation">;</span>
            <span class="token keyword">proxy_busy_buffers_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
            <span class="token keyword">proxy_temp_file_write_size</span> <span class="token number">64</span>k<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
<span class="token comment"># 然后保存并退出</span>

<span class="token comment"># 启动负载均衡服务器192.168.0.4（Nginx-Server）</span>
docker run <span class="token operator">-</span>d <span class="token operator">-</span>p <span class="token number">8888</span><span class="token punctuation">:</span><span class="token number">80</span> <span class="token operator">--</span>name nginx<span class="token operator">-</span><span class="token keyword">server</span> <span class="token operator">-</span>v <span class="token operator">/</span>opt<span class="token operator">/</span>confs<span class="token operator">/</span>nginx<span class="token punctuation">.</span>conf<span class="token punctuation">:</span><span class="token operator">/</span>etc<span class="token operator">/</span>nginx<span class="token operator">/</span>nginx<span class="token punctuation">.</span>conf <span class="token operator">--</span>restart always nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><h3 id="操作对象-192-168-0-5-nginx-node1-nginx-web1"><a href="#操作对象-192-168-0-5-nginx-node1-nginx-web1" class="header-anchor">#</a> 操作对象：192.168.0.5（Nginx-Node1/Nginx-Web1）</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment"># 创建文件夹用于存放web页面</span>
$ mkdir <span class="token operator">-</span>p <span class="token operator">/</span>opt<span class="token operator">/</span>html
$ vim <span class="token operator">/</span>opt<span class="token operator">/</span>html<span class="token operator">/</span><span class="token keyword">index</span><span class="token punctuation">.</span>html

<span class="token comment"># 编辑内容如下：</span>
<span class="token operator">&lt;</span>div<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span>h1<span class="token operator">&gt;</span>
    The host is <span class="token number">192.168</span><span class="token number">.0</span><span class="token punctuation">.</span><span class="token function">5</span><span class="token punctuation">(</span>Docker02<span class="token punctuation">)</span> <span class="token operator">-</span> Node <span class="token number">1</span><span class="token operator">!</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>h1<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token comment"># 然后保存并退出</span>

<span class="token comment"># 启动192.168.0.5（Nginx-Node1/Nginx-Web1）</span>
$ docker run <span class="token operator">-</span>d <span class="token operator">-</span>p <span class="token number">6666</span><span class="token punctuation">:</span><span class="token number">80</span> <span class="token operator">--</span>name nginx<span class="token operator">-</span>node1 <span class="token operator">-</span>v <span class="token operator">/</span>opt<span class="token operator">/</span>html<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html <span class="token operator">--</span>restart always nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h3 id="操作对象-192-168-0-7-nginx-node2-nginx-web2"><a href="#操作对象-192-168-0-7-nginx-node2-nginx-web2" class="header-anchor">#</a> 操作对象：192.168.0.7（Nginx-Node2/Nginx-Web2）</h3> <div class="language-nginx line-numbers-mode"><pre class="language-nginx"><code><span class="token comment"># 创建文件夹用于存放web页面</span>
$ mkdir <span class="token operator">-</span>p <span class="token operator">/</span>opt<span class="token operator">/</span>html
$ vim <span class="token operator">/</span>opt<span class="token operator">/</span>html<span class="token operator">/</span><span class="token keyword">index</span><span class="token punctuation">.</span>html

<span class="token comment"># 编辑内容如下：</span>
<span class="token operator">&lt;</span>div<span class="token operator">&gt;</span>
  <span class="token operator">&lt;</span>h1<span class="token operator">&gt;</span>
    The host is <span class="token number">192.168</span><span class="token number">.0</span><span class="token punctuation">.</span><span class="token function">7</span><span class="token punctuation">(</span>Docker03<span class="token punctuation">)</span> <span class="token operator">-</span> Node <span class="token number">2</span><span class="token operator">!</span>
  <span class="token operator">&lt;</span><span class="token operator">/</span>h1<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>div<span class="token operator">&gt;</span>
<span class="token comment"># 然后保存并退出</span>

<span class="token comment"># 启动192.168.0.7（Nginx-Node2/Nginx-Web2）</span>
$ docker run <span class="token operator">-</span>d <span class="token operator">-</span>p <span class="token number">6666</span><span class="token punctuation">:</span><span class="token number">80</span> <span class="token operator">--</span>name nginx<span class="token operator">-</span>node2 <span class="token operator">-</span>v $<span class="token punctuation">(</span>pwd<span class="token punctuation">)</span><span class="token operator">/</span>html<span class="token punctuation">:</span><span class="token operator">/</span>usr<span class="token operator">/</span>share<span class="token operator">/</span>nginx<span class="token operator">/</span>html <span class="token operator">--</span>restart always nginx
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><p>测试：域名:mongo.demo.com，这里是用Windows系统主机访问服务器，要在当前主机的hosts中添加解析 “mongo.demo.com 192.168.0.4”，hosts文件所在的路径为 “C:\Windows\System32\drivers\etc”。这里在Windows主机上通过浏览器访问 “http://mongo.demo.com:8888” 这个站点的时候，Nginx会根据来访的主机的ip_hash值，负载均衡到192.168.0.5（Nginx-Node1/Nginx-Web1）和192.168.0.7（Nginx-Node2/Nginx-Web2）服务器上。</p></div> <footer class="page-edit" style="display:none;"><!----> <div class="last-updated"><span class="prefix">Last Updated: </span> <span class="time">4/16/2021, 6:55:27 PM</span></div></footer> <!----> <!----></main> <!----> <div class="comments-wrapper" data-v-e3b9d544><div class="valine-wrapper"><div id="valine"></div></div></div></div></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;background-color:rgba(231, 234, 241,.5);display:none;" data-v-44bd5a18 data-v-44bd5a18><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-44bd5a18><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-44bd5a18></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-44bd5a18></path></svg></div><div data-v-932dc3c2></div></div></div>
    <script src="/assets/js/app.f4a44a18.js" defer></script><script src="/assets/js/3.48232c0c.js" defer></script><script src="/assets/js/1.1ffd113d.js" defer></script><script src="/assets/js/32.4e2b272d.js" defer></script>
  </body>
</html>
